<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <input type="checkbox" id="checkedAll"> 全选
  <button class="btn">反选</button>
  <ul>
    <li><input type="checkbox" class="ck">张三</li>
    <li><input type="checkbox" class="ck">李四</li>
    <li><input type="checkbox" class="ck">王五</li>
  </ul>
  <script>
    const checkedAll=document.querySelector('#checkedAll')
    const cks=document.querySelectorAll('.ck')
    const btn=document.querySelector('.btn')
    checkedAll.addEventListener('click',function(){
     
      for (let i = 0; i < cks.length; i++) {
        cks[i].checked=this.checked
      }
    })
    for (let i = 0; i < cks.length; i++) {
        cks[i].addEventListener('click',function(){
          checkedAll.checked=document.querySelectorAll('.ck:checked').length===cks.length
        })

      }
      for (let i = 0; i < cks.length; i++) {
       btn.addEventListener('click',function(){
        console.log(btn);
        if (cks[i].checked===false) {
         cks[i].checked=true
        }else{
          cks[i].checked=false
        }
        })

      }
  </script>
</body>
</html>